package user

import (
	"context"
	"fast-boot/app/model"
	"fast-boot/common/globalkey"
	"github.com/hwUltra/fb-tools/jwtx"
	"github.com/jinzhu/copier"

	"fast-boot/app/api/wxapp/internal/svc"
	"fast-boot/app/api/wxapp/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取用户信息
func NewMeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MeLogic {
	return &MeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MeLogic) Me() (*types.UserInfo, error) {
	u := &model.UserModel{}
	uid := jwtx.NewJwtX(l.ctx, l.svcCtx.Config.JwtUserKey).GetUid()
	l.svcCtx.GormClient.GormDb.First(&u, "id = ?", uid)
	resp := types.UserInfo{}
	if err := copier.CopyWithOption(&resp, u, globalkey.CopierProtoOptions); err != nil {
		return nil, err
	}
	return &resp, nil
}
